import $ from 'jquery';

//设置AJAX请求头
$.support.cors = true;
$.ajaxSetup({
    async: true,
    cache: false,
    timeout: 30000, //超时时间：30秒
    dataType: "json",
    beforeSend: request => {
        //request.setRequestHeader("Authorization", window.localStorage["Token"]);
        request.setRequestHeader("Cache-Control", 'no-store');
        request.setRequestHeader("Content-Type", 'application/json;charset=utf-8');
    }
});

const AjaxCall = async (url, type, data = {}) => {
    type = typeof (type) != "undefined" ? type.toLowerCase() : 'get';
    data = type === 'post' ? JSON.stringify(data) : data;
    return await $.ajax({
        type: type,
        url: url,
        data: data,
        success: res => { return res; },
        error: (err) => {
            debugger;
            const error = `AjaxError：
                               status：${err.status}
                               statusText：${err.statusText}
                               url：${url}
                               type：${type}
                               data：${typeof (data) == "string" ? data : JSON.stringify(data)}\r\n`;
            console.log(error, JSON.stringify(err));
            //alert(error);
        }
    });
}

//调用示例
// AjaxCall("http://localhost:40301/api/Default1/Feach", "post", { username: "daea", password: "password" }).then(res => console.log(alert(res)));
//AjaxCall("http://localhost:40301/api/Default1/Feach").then(res => console.log(alert(res)));

export default AjaxCall;